package com.inwavetech.os;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.inwavetech.os.entity.Os;
import com.inwavetech.os.sync.OsSender;
import com.inwavetech.os.util.entityfields.FinishOsFields;
import com.inwavetech.os.util.field.dynamicfields.EntityUpdatableFields;

import java.sql.SQLException;
import java.util.Date;

public class FinishOsActivity extends BaseOsCheckActivity {

    private EntityUpdatableFields entityFields;
    private TextView signatureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finish_os);

        // get the os and check the user ownership
        final Os os = getUserOs();

        if (os == null) {
            backToMain();

            return;
        }

        // configure the fields
        entityFields = new EntityUpdatableFields(
                os,
                new FinishOsFields(this, (ViewGroup) findViewById(R.id.container))
        );

        signatureView = (TextView) findViewById(R.id.signature);

        // configure the bts
        Button signatureBt = (Button) findViewById(R.id.signature_bt);
        Button finishBt = (Button) findViewById(R.id.finish_bt);

        signatureBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // trigger the signature activity
                gotoSignatureActivity(os.id);
            }
        });

        finishBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // check the signature
                if (os.signature_filename == null) {
                    // feedback
                    userMessage(getString(R.string.finish_no_signature));
                    // do nothing
                    return;
                }

                // put on the send queue
                os.status = Os.Status.FINISHED;
                os.send_to_server = true;
                os.updated_at = new Date();

                updateOs(os);

                (new TrySending()).execute(os);

                // redirect to the detail view
                Intent i = buildOsDetailIntent(os.id);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();

        // get the os and check the user ownership
        Os os = getUserOs();


        if(os == null) {
            backToMain();

            return;
        }

        try {
            entityFields.updateFields();
        } catch (Exception e) {
            Log.i(FinishOsActivity.class.getName(), "bad configured views/entity (fields)");

            backToMain();

            return;
        }

        if (os.signature_filename != null) {
            signatureView.setText("OK");
        }

    }

    class TrySending extends AsyncTask<Os, Object, Void> {

        @Override
        protected Void doInBackground(Os[] oses) {
            os = oses[0];
            OsSender osSender = new OsSender(getApplicationContext(), getSessionManager());
            os.send_to_server = false;

            try {
                osSender.send(os);
                osDao.update(os);
            } catch (Exception e) {
                logError(e);
            }

            return null;
        }
    }

}
